package gui;

import gui.crud.ClassEditPanel;
import gui.crud.ClassRegisterPanel;
import gui.crud.DisciplineEditPanel;
import gui.crud.DisciplineRegisterPanel;
import gui.crud.ProfessorEditPanel;
import gui.crud.ProfessorRegisterPane;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Menu extends JPanel {

	private JButton professorButton, disciplineButton, classButton, toProfessorRegisterPane,
	toDisciplineRegisterPane, toClassRegisterPane, toProfessorEditPane,
	toDisciplineEditPane, toClassEditPane;

	private String title = "Menu - Pythagoras";

	public Menu() {
		this.setSize(400, 400);
		this.setLayout(null);

		professorButton = new JButton("Professor");
		disciplineButton = new JButton("Disciplina");
		classButton = new JButton("Turma");
		toProfessorRegisterPane = new JButton("Cadastrar Professor");
		toDisciplineRegisterPane = new JButton("Cadastrar Disciplina");
		toClassRegisterPane = new JButton("Criar turma");
		toProfessorEditPane = new JButton("Editar Professor");
		toDisciplineEditPane = new JButton("Editar Disciplina");
		toClassEditPane = new JButton("Editar Turma");

		professorButton.setBounds(115, 70, 150, 35);
		disciplineButton.setBounds(115, 150, 150, 35);
		classButton.setBounds(115, 230, 150, 35);
		toProfessorRegisterPane.setBounds(45, 110, 135, 25);
		toDisciplineRegisterPane.setBounds(45, 190, 135, 25);
		toClassRegisterPane.setBounds(45, 270, 135, 25);
		toProfessorEditPane.setBounds(195, 110, 135, 25);
		toDisciplineEditPane.setBounds(195, 190, 135, 25);
		toClassEditPane.setBounds(195, 270, 135, 25);

		Font font = toProfessorRegisterPane.getFont().deriveFont(10.0f);
		toProfessorRegisterPane.setFont(font);
		toDisciplineRegisterPane.setFont(font);
		toClassRegisterPane.setFont(font);
		toProfessorEditPane.setFont(font);
		toDisciplineEditPane.setFont(font);
		toClassEditPane.setFont(font);

		toProfessorRegisterPane.setVisible(false);
		toDisciplineRegisterPane.setVisible(false);
		toClassRegisterPane.setVisible(false);
		toProfessorEditPane.setVisible(false);
		toDisciplineEditPane.setVisible(false);
		toClassEditPane.setVisible(false);

		professorButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (toProfessorEditPane.isVisible()) {
					toProfessorEditPane.setVisible(false);
					toProfessorRegisterPane.setVisible(false);
				} else {
					toProfessorEditPane.setVisible(true);
					toProfessorRegisterPane.setVisible(true);
				}
			}
		});
		
		disciplineButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (toDisciplineEditPane.isVisible()) {
					toDisciplineEditPane.setVisible(false);
					toDisciplineRegisterPane.setVisible(false);
				} else {
					toDisciplineEditPane.setVisible(true);
					toDisciplineRegisterPane.setVisible(true);
				}
			}
		});
		
		classButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (toClassEditPane.isVisible()) {
					toClassEditPane.setVisible(false);
					toClassRegisterPane.setVisible(false);
				} else {
					toClassEditPane.setVisible(true);
					toClassRegisterPane.setVisible(true);
				}
			}
		});

		toProfessorRegisterPane.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				openProfessorRegisterPane();
			}
		});

		toDisciplineRegisterPane.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				openDisciplineRegisterPane();
			}
		});

		toClassRegisterPane.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				openClassRegisterPane();
			}
		});

		toProfessorEditPane.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				openProfessorEditPane();
			}
		});

		toDisciplineEditPane.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				openDisciplineEditPane();
			}
		});

		toClassEditPane.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				openClassEditPane();
			}
		});
		
		this.add(toProfessorRegisterPane);
		this.add(toDisciplineRegisterPane);
		this.add(toClassRegisterPane);
		this.add(toProfessorEditPane);
		this.add(toDisciplineEditPane);
		this.add(toClassEditPane);
		this.add(professorButton);
		this.add(disciplineButton);
		this.add(classButton);

	}

	protected void openClassRegisterPane() {
		ClassRegisterPanel c = new ClassRegisterPanel();
		((AbstractFramePythagoras) this.getTopLevelAncestor()).changePanel(c, c.getTitle());
	}

	protected void openDisciplineRegisterPane() {
		DisciplineRegisterPanel d = new DisciplineRegisterPanel();
		((AbstractFramePythagoras) this.getTopLevelAncestor()).changePanel(d, d.getTitle());
	}

	protected void openProfessorRegisterPane() {
		ProfessorRegisterPane p = new ProfessorRegisterPane();
		((AbstractFramePythagoras) this.getTopLevelAncestor()).changePanel(p, p.getTitle());
	}
	
	protected void openClassEditPane() {
		ClassEditPanel c = new ClassEditPanel();
		((AbstractFramePythagoras) this.getTopLevelAncestor()).changePanel(c, c.getTitle());
	}

	protected void openDisciplineEditPane() {
		DisciplineEditPanel d = new DisciplineEditPanel();
		((AbstractFramePythagoras) this.getTopLevelAncestor()).changePanel(d, d.getTitle());
	}

	protected void openProfessorEditPane() {
		ProfessorEditPanel p = new ProfessorEditPanel();
		((AbstractFramePythagoras) this.getTopLevelAncestor()).changePanel(p, p.getTitle());
	}

	public String getTitle() {
		return title;
	}

	protected void setTitle(String title) {
		this.title = title;
	}

}
